package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Elastic;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;

	public class FWBSlot
	{
		public var bonusItem:*;
		
		public var display:Sprite = new Sprite();
		private var m_snUser:SNUser;
		public var score:Number = 0;
		private var updatedScore:Number = 0;
		private var m_scoreTF:TextField;
		private var m_prize:Sprite;
		private var container:Sprite = new Sprite();
		private var m_messageTF:TextField;
		
		public static const PADDING:int = 10;
		
		private var m_scoreUpdater:Timer = new Timer(1000/24);
		public static const WIDTH:int = 350;
		public static const HEIGHT:int = 60;
		
		
		
		public function FWBSlot(snUser:SNUser, score:int)
		{
			this.score = score;
			m_scoreUpdater.addEventListener(TimerEvent.TIMER, onScoreUpdateTick);
			display = Util.getRoundRectSprite(WIDTH,HEIGHT,0,.5,20);
			m_snUser = snUser;
			var profilePic:DisplayObject = ImageManager.getImage(snUser.img);
			container.addChild(profilePic);
			profilePic.x = WIDTH - profilePic.width - PADDING;
			profilePic.y = HEIGHT/2 - profilePic.height/2;
			
			m_prize = Util.getRoundRectSprite(Game.PPS,Game.PPS, 0xff7777,.8);
			container.addChild(m_prize);
			m_prize.x = PADDING;
			m_prize.y = HEIGHT/2 - m_prize.height/2;
			m_prize.addEventListener(MouseEvent.CLICK, onPrizeClick);
			
			m_scoreTF = new TextField();
			m_scoreTF.selectable = false;
			Util.applyStyleH2(m_scoreTF);
			updateScoreTextField();
			container.addChild(m_scoreTF);
			m_scoreTF.x = m_prize.x + m_prize.width + PADDING;
			m_scoreTF.y = PADDING;
			
			m_messageTF = new TextField();
			m_messageTF.selectable = false;
			Util.applyStyleH3(m_messageTF,0xffff00);
			container.addChild(m_messageTF);
			m_messageTF.x = m_prize.x + m_prize.width + PADDING;
			m_messageTF.y = 32;
			
			
			
			display.addChild(container);
		}
		
		
		private function onPrizeClick(me:MouseEvent):void{
			Util.removeChildren(m_prize);
			switch(true){
				case (bonusItem is GameObject):
					Game.instance.overrideNextItem(bonusItem);
					break;
				
			}
		}
		
		public function udpateScore(newScore:Number):void{
			updatedScore = newScore;
			m_scoreUpdater.start();
		}
		
		private function onScoreUpdateTick(te:TimerEvent):void{
			var delta:Number = updatedScore - score;
			if(delta < .5){
				m_scoreUpdater.stop();
			}
			score += delta*.25;
			updateScoreTextField();
		}
		
		private function updateScoreTextField():void{
			m_scoreTF.text = String(Math.round(score));
			m_scoreTF.autoSize = TextFieldAutoSize.LEFT;			
		}
		
		public function updateMessageField(message:String):void{

			TweenLite.to(m_messageTF, .5, {alpha: 0,
				onComplete: function():void{
					m_messageTF.text = message;
					m_messageTF.autoSize = TextFieldAutoSize.LEFT;	
					TweenLite.to(m_messageTF, 1, {alpha: 1});
				}
			});
		}
		
		public function applyBonusItem(item:DisplayObject):void{
			item.filters = [new GlowFilter(0, 1, 3,3,5)];
			Util.removeChildren(m_prize);
			var itemEndingLocation:Point = new Point(m_prize.width/2 - item.width/2, m_prize.height/2 - item.height/2);
			m_prize.addChild(item);
			
			Util.scale(item, 3, 1, 1, 0, 1,Bounce.easeOut);
//			item.alpha = 0;
//			TweenLite.to(item, 1, {alpha: 1});
		}
		
	}
}